package nc.tb.report.plugin;

import java.util.ArrayList;
import java.util.Arrays;

import nc.tb.cube.cell.ICellType;
import nc.tb.report.CellWrap;
import nc.tb.report.ViewUIModel;

import com.ufsoft.report.UfoReport;
import com.ufsoft.table.Cell;

public class TbRowHeaderLvlCmd extends TbCommand {
	
	private UfoReport m_report;
	private boolean isLvlUp = false;
	
	public TbRowHeaderLvlCmd(UfoReport m_report, boolean isLvlUp) {
		this.m_report = m_report;
		this.isLvlUp = isLvlUp;
	}
	
	@Override
	public void execute(Object[] params) {
		TbReport report = (TbReport) m_report;		
		Cell[] targetCells = getSelectedCells(report);//report.getSelectedCells();
		if (targetCells == null)
			return;
		ArrayList<Cell> al = new ArrayList<Cell>();
		int count = ViewUIModel.TAB_OCCUPYER.length() / ViewUIModel.TAB_OCCUPYER_1.length();
		count = isLvlUp ? count : count*(-1);
		if (isLvlUp) {
			for (int i = 0; i < targetCells.length; i++) {
				CellWrap cw = targetCells[i]==null ? null : (CellWrap)targetCells[i].getExtFmt("tb");
				if (cw == null || cw.getCellType() != ICellType.ROWHEADCELL)
					continue;
				int countN = getTabSize(targetCells[i].getValue());
				count = Math.min(count, countN);
				if (countN > 0 || count < 0)
					al.add(targetCells[i]);
	//			changeCellLevles(targetCells[i], isLvlUp ? 1 : -1);
			}
		}
		else {
			al.addAll(Arrays.asList(targetCells));
			count = -1;
		}
		if (count != 0 & !al.isEmpty()) {
			for (Cell cell : al) {
				changeCellLevles(cell, count);
			}
		}
		m_report.repaint();
	}
	
	private int getTabSize(Object value) {
		if (!isLvlUp)
			return -1;
		String strValue = value==null ? null : value.toString();
		if (strValue == null)
			return 0;
		int mLength = ViewUIModel.TAB_OCCUPYER_1.length();
		int count = 0;
		while (strValue.startsWith(ViewUIModel.TAB_OCCUPYER_1)) {
			strValue = strValue.substring(mLength);
			count ++;
		}
		return count;
	}
	
	private static void changeCellLevles(Cell dst, int levelOffset) {
		if(dst == null || levelOffset == 0)
			return ;
		String strValue = (String)dst.getValue();
		if(strValue == null || strValue.trim().length() == 0)
			return ;
		boolean bPromoteFlag = true;
		if(levelOffset < 0){
			bPromoteFlag = false;
			levelOffset = levelOffset * (-1);
		}
		
		if(bPromoteFlag){
			int mLength = ViewUIModel.TAB_OCCUPYER_1.length();
			for(int i = 0; i < levelOffset && strValue.startsWith(ViewUIModel.TAB_OCCUPYER_1); i ++){
				strValue = strValue.substring(mLength);
			}
//			int nLength = ViewUIModel.TAB_OCCUPYER.length();
//			for(int i = 0; i < levelOffset && strValue.startsWith(ViewUIModel.TAB_OCCUPYER); i ++){
//				strValue = strValue.substring(nLength);
//			}
		}else{
			StringBuffer sbWork = new StringBuffer();
			for(int i = 0; i < levelOffset; i ++){
				sbWork.append(ViewUIModel.TAB_OCCUPYER);
			}
			sbWork.append(strValue);
			strValue = sbWork.toString();
		}
		dst.setValue(strValue);
	}

}
